package com.cloud.phone.utils

import android.content.ContentValues
import android.content.Context
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import com.blankj.utilcode.util.LogUtils
import com.cloud.phone.base.BaseApp
import com.cloud.phone.constants.Constants
import com.cloud.phone.manager.FileManager
import com.cloud.phone.task.SendMsgUtils
import com.cloud.phone.task.bean.ProxyInfoBean
import com.cloud.phone.view.TsUtils
import java.io.File
import java.io.FileInputStream
import java.io.IOException


/**
 * @auth: njb
 * @date: 2024/6/28 15:41
 * @desc: 描述
 */
object MediaScannerHelper {
    private val TAG = "MediaScannerHelper"

    /**
     * 刷新单个文件到相册
     */
    fun updateToGallery(photoFile: File, context: Context) {
        LogUtils.d(TAG, "===刷新的文件为===$photoFile")
        val contentValues = ContentValues().apply {
            put(MediaStore.Images.Media.DISPLAY_NAME, "photo.jpg")
            put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                put(MediaStore.Images.Media.IS_PENDING, 1)
            }
        }

        val contentResolver = context.contentResolver
        val collectionUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
        } else {
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        }

        val imageUri = contentResolver.insert(collectionUri, contentValues)

        imageUri?.let { uri ->
            try {
                contentResolver.openOutputStream(uri)?.use { outputStream ->
                    val inputStream = FileInputStream(photoFile)
                    inputStream.copyTo(outputStream)
                    inputStream.close()
                    outputStream.close()
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    contentValues.clear()
                    contentValues.put(MediaStore.Images.Media.IS_PENDING, 0)
                    contentValues.put(
                        MediaStore.Images.Media.RELATIVE_PATH,
                        FileManager.SAVE_MEDIA_ROOT_DIR
                    )
                    contentResolver.update(uri, contentValues, null, null)
                    TsUtils.showTips("===刷新相册成功===")
                }
            } catch (e: IOException) {
                e.printStackTrace()
                LogUtils.e(TAG, "===刷新相册失败===" + e.message)
                TsUtils.showTips("===刷新相册失败===" + e.message)
            }
        }
    }

    /**
     * 刷新整个相册
     */
    fun refreshFolder(folderPath: String, proxyInfoBean: ProxyInfoBean) {
        LogUtils.i(TAG, "===文件目录为===${folderPath}")
        try {
            MediaScannerConnection.scanFile(BaseApp.app, arrayOf(folderPath), null) { path, uri ->
                LogUtils.i(TAG, "文件刷新成功 Scanned $path: $uri")
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}